load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("//tools/lint:linters.bzl", "shellcheck_test")
load(":cmake_text_tests.bzl", "cmake_script_test_suite")
load(":convert_shell_script_test.bzl", "shell_script_conversion_suite")
load(":shell_script_helper_test_rule.bzl", "shell_script_helper_test_rule")
load(":symlink_contents_to_dir_test_rule.bzl", "symlink_contents_to_dir_test_rule")
load(":utils_test.bzl", "utils_test_suite")

# gazelle:exclude

TOOL_TARGETS = [
    "//toolchains/private:cmake_tool",
    "//toolchains/private:make_tool",
    "//toolchains/private:meson_tool",
    "//toolchains/private:ninja_tool",
    "//toolchains/private:pkgconfig_tool",
]

filegroup(
    name = "build_scripts",
    testonly = True,
    srcs = TOOL_TARGETS,
    output_group = "script_file",
)

filegroup(
    name = "wrapper_scripts",
    testonly = True,
    srcs = TOOL_TARGETS,
    output_group = "wrapper_script_file",
)

sh_library(
    name = "tool_build_scripts",
    testonly = True,
    srcs = [
        ":build_scripts",
        ":wrapper_scripts",
    ],
    tags = ["lint-genfiles"],
)

shellcheck_test(
    name = "shellcheck_linux",
    srcs = [":tool_build_scripts"],
    target_compatible_with = [
        "@platforms//os:linux",
    ],
)

shellcheck_test(
    name = "shellcheck_macos",
    srcs = [":tool_build_scripts"],
    target_compatible_with = [
        "@platforms//os:macos",
    ],
)

cmake_script_test_suite()

shell_script_conversion_suite()

utils_test_suite()

shell_script_helper_test_rule(
    name = "shell_script_inner_fun",
    out = "inner_fun_text.txt",
    script = ["##symlink_contents_to_dir## $$SOURCE_DIR$$ $$TARGET_DIR$$ False"],
)

# TODO: This should not be necessary but there appears to be some inconsistent
# behavior with the use of `constraint_value`s in `select` statements. A support
# thread was started at the end of https://github.com/bazelbuild/bazel/pull/12071
# Once it is possible to replace `:macos` with `@platforms//os:macos` that
# should be done for this file. Note actioning on this will set the minimum
# supported version of Bazel to 4.0.0 for these examples.
config_setting(
    name = "macos",
    constraint_values = ["@platforms//os:macos"],
    visibility = ["//visibility:private"],
)

config_setting(
    name = "freebsd",
    constraint_values = ["@platforms//os:freebsd"],
    visibility = ["//visibility:private"],
)

diff_test(
    name = "shell_script_inner_fun_test",
    file1 = ":shell_script_inner_fun",
    file2 = select({
        ":freebsd": "expected/inner_fun_text_freebsd.txt",
        ":macos": "expected/inner_fun_text_macos.txt",
        "//conditions:default": "expected/inner_fun_text.txt",
    }),
)

filegroup(
    name = "dir1_fg",
    srcs = glob(["dir1/**"]),
)

filegroup(
    name = "dir2_fg",
    srcs = glob(["dir2/**"]),
)

symlink_contents_to_dir_test_rule(
    name = "symlink_dirs",
    out = "out_symlinked_dirs.txt",
    dir1 = ":dir1_fg",
    dir2 = ":dir2_fg",
)

diff_test(
    name = "shell_method_symlink_contents_to_dir_test",
    file1 = select({
        ":freebsd": "expected/out_symlinked_dirs_freebsd.txt",
        ":macos": "expected/out_symlinked_dirs_macos.txt",
        "//conditions:default": "expected/out_symlinked_dirs.txt",
    }),
    file2 = ":symlink_dirs",
)
